package com.rpc.example.spring.service;

import com.rpc.example.RegistoryFacory;
import com.rpc.example.constant.RegisterType;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;

import java.net.UnknownHostException;

/**
 * @author Elvis0
 * @version 1.0, 2022/11/17
 */
@AutoConfiguration
@EnableConfigurationProperties({ServiceProviderConfiguration.class})
public class RpcAutoConfiguration {

    @Bean
    public RpcProviderBean rpcBeanProvider(ServiceProviderConfiguration configuration) throws Exception {
        try {
            return new RpcProviderBean(configuration.getHostAddress(), configuration.getServicePort(), configuration.getApplicationName(),
                    RegistoryFacory.createRegisterService(configuration.getRegisterAddress(), RegisterType.findByCode(configuration.getRegistryType())));
        } catch (UnknownHostException e) {
            throw new RuntimeException(e);
        }
    }
}
